#include "widget.h"
#include "ui_widget.h"

#include <QTreeWidgetItem>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 带头部
    // 设置头部
    ui->treeWidget->setHeaderLabels({"英雄类型", "英雄介绍", "备注"});


    // 设置顶级节点
    QTreeWidgetItem* p = new QTreeWidgetItem({"力量", "力量很大的英雄"});
    // ui->treeWidget->addTopLevelItem(p);
    QTreeWidgetItem* m = new QTreeWidgetItem({"敏捷", "动作很快的英雄"});
    QTreeWidgetItem* a = new QTreeWidgetItem({"智力", "脑子很灵光的英雄"});

    ui->treeWidget->addTopLevelItems({p, m, a});

    // 设置二级节点
    QTreeWidgetItem* niu = new QTreeWidgetItem({"牛头", "长得像牛的英雄", "能把人打晕"});
    p->addChild(niu);
    QTreeWidgetItem* shan = new QTreeWidgetItem({"山丘", "会扔锤子的英雄", "锤子能把人砸晕"});
    p->addChild(shan);

    QTreeWidgetItem* jian = new QTreeWidgetItem({"剑圣", "拿把大刀乱砍的敏捷型英雄"});
    m->addChild(jian);
    QTreeWidgetItem* em = new QTreeWidgetItem({"恶魔猎手", "拿把水果刀乱砍的敏捷型英雄"});
    m->addChild(em);

    QTreeWidgetItem* dfs = new QTreeWidgetItem({"大法师", "骑着马到处乱逛的法师"});
    a->addChild(dfs);
    QTreeWidgetItem* xianzhi = new QTreeWidgetItem({"先知", "会放电的法师"});
    a->addChild(xianzhi);

    // 不带头部
    ui->deptWidget->setHeaderHidden(true);

    // 设置顶级节点--->财务部
    QTreeWidgetItem* money = new QTreeWidgetItem(ui->deptWidget);
    money->setText(0, "财务部");
    money->setCheckState(0, Qt::Unchecked);
    ui->deptWidget->addTopLevelItem(money);

    // 设置顶级节点--->技术部
    QTreeWidgetItem* tec = new QTreeWidgetItem(ui->deptWidget);
    tec->setText(0, "技术部");
    tec->setCheckState(0, Qt::Unchecked);
    ui->deptWidget->addTopLevelItem(tec);

    // 在技术部中设置子节点
    QTreeWidgetItem* dev = new QTreeWidgetItem;
    dev->setText(0, "研发部");
    dev->setCheckState(0, Qt::Checked);
    tec->addChild(dev);

    QTreeWidgetItem* test = new QTreeWidgetItem;
    test->setText(0, "测试部");
    test->setCheckState(0, Qt::Unchecked);
    tec->addChild(test);

    // 设置顶级节点--->人事行政部
    QTreeWidgetItem* pers = new QTreeWidgetItem(ui->deptWidget);
    pers->setText(0, "人事行政部");
    pers->setCheckState(0, Qt::Unchecked);
    ui->deptWidget->addTopLevelItem(pers);

    // 在人事行政部中设置子节点
    QTreeWidgetItem* hr = new QTreeWidgetItem;
    hr->setText(0, "人事部");
    hr->setCheckState(0, Qt::Checked);
    pers->addChild(hr);

    QTreeWidgetItem* adm = new QTreeWidgetItem;
    adm->setText(0, "行政部");
    adm->setCheckState(0, Qt::Checked);
    pers->addChild(adm);

    // 在行政部中设置子节点
    QTreeWidgetItem* adm1 = new QTreeWidgetItem;
    adm1->setText(0, "行政一部");
    adm1->setCheckState(0, Qt::Checked);
    adm->addChild(adm1);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_deptWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    qDebug() << column << item->text(column);
    item->setCheckState(0, Qt::Checked);
}

// 子菜单展开时触发
void Widget::on_deptWidget_itemExpanded(QTreeWidgetItem *item)
{
    qDebug() << item->text(0);
}

// 子菜单折叠时触发
void Widget::on_deptWidget_itemCollapsed(QTreeWidgetItem *item)
{
    qDebug() << item->text(0);
}


